package cn.com.ghy.android.widget.report;

import java.util.List;

import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
import cn.com.ghy.android.bean.CustomerInfoBean;
import cn.com.ghy.android.bean.OrderDetailBean;
import cn.com.ghy.android.util.CommonStatic;
import cn.com.ghy.android.widget.R;

/**
 * @软件著作权:金红叶纸业集团有限公司
 * @系统名称:SFA系统
 * @Description:订单查询信息页面
 * @author wuz  @date 2011-5-20 13:49:55
 * @版本历史:v1.0
 * @Copyright:Copyright (c) 2011-2012 
 */

public class OrderInfoView extends TabActivity {

	private TextView labTxtOrderGoodsDate = null;
	private TextView labTxtOrderTotalPrice = null;
	private TextView labTxtOrderTotalPriceNoRate = null;
	private TextView labTxtOrderSendTotalPrice = null;
	private TextView labTxtOrderSendTotalPriceNoRate = null;
	private TextView labTxtCustomerId = null;
	private TextView labTxtCustomerName = null;
	private TextView labTxtOrderId = null;
	private TextView labTxtHandOrderId = null;
	private CheckBox chkIsCar= null;
//	private TextView labTxtContact = null;
	private TextView labTxtPhone = null;
	private TextView labTxtSendGoodsAddress = null;
	private TextView labTxtFillOrderDate = null;
	private TextView labTxtFaxstate = null;
	private TextView labTxtFax = null;
	private Button btnFax = null;

	private ListView listView = null;
	private LinearLayout layChangeBusiness = null;

	private Intent intent = null;
	private Bundle bundle = null;
	private CustomerInfoBean customerInfo = null;
	private List<OrderDetailBean> listOrderDetail = null;

	private OrderInfoAdapter orderInfoAdapter = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.order_info_view);
		getSession();
		TabHost tabHost = this.getTabHost();
		TabWidget tabWidget = tabHost.getTabWidget();

		tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("基本信息")
				.setContent(R.id.layOrderBaseInfo));
		tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("订单明细")
				.setContent(R.id.layOrderDetail));

		for (int i = 0; i < tabWidget.getChildCount(); i++) {
			tabWidget.getChildAt(i).getLayoutParams().height = this.getWindowManager()
					.getDefaultDisplay().getHeight() / 16;
		}
		findView();
		init();
		addListener();
	}

	@SuppressWarnings("unchecked")
	public void getSession() {
		intent = this.getIntent();
		customerInfo = (CustomerInfoBean) intent.getSerializableExtra("customerInfo");
		listOrderDetail = (List<OrderDetailBean>) intent.getSerializableExtra("listOrderDetail");
	}

	public void findView() {
		labTxtOrderGoodsDate = (TextView) this.findViewById(R.id.labTxtOrderGoodsDate);
		labTxtOrderTotalPrice = (TextView) this.findViewById(R.id.labTxtOrderTotalPrice);
		labTxtCustomerId = (TextView) this.findViewById(R.id.labTxtCustomerId);
		labTxtCustomerName = (TextView) this.findViewById(R.id.labTxtCustomerName);

//		labTxtContact = (TextView) this.findViewById(R.id.labTxtContact);
		labTxtPhone = (TextView) this.findViewById(R.id.labTxtPhone);
		labTxtSendGoodsAddress = (TextView) this.findViewById(R.id.labTxtSendGoodsAddress);

		labTxtHandOrderId = (TextView) this.findViewById(R.id.labTxtHandOrderId);
		chkIsCar = (CheckBox) this.findViewById(R.id.chkIsCar);
		labTxtFillOrderDate = (TextView) this.findViewById(R.id.labTxtFillOrderDate);
		labTxtOrderId = (TextView) this.findViewById(R.id.labTxtOrderId);
		listView = (ListView) this.findViewById(R.id.listOrderDetailList);
		layChangeBusiness = (LinearLayout) this.findViewById(R.id.layChangeBusiness);
		labTxtFaxstate = (TextView) this.findViewById(R.id.labTxtFaxstate);
		labTxtFax = (TextView) this.findViewById(R.id.labTxtFax);
		btnFax = (Button) this.findViewById(R.id.btnFax);

		labTxtOrderTotalPriceNoRate = (TextView) this
				.findViewById(R.id.labTxtOrderTotalPriceNoRate);
		;
		labTxtOrderSendTotalPrice = (TextView) this.findViewById(R.id.labTxtOrderSendTotalPrice);
		;
		labTxtOrderSendTotalPriceNoRate = (TextView) this
				.findViewById(R.id.labTxtOrderSendTotalPriceNoRate);
		;
	}

	public void init() {
		labTxtCustomerId.setText(customerInfo.getPartner());
		labTxtCustomerName.setText(customerInfo.getName_org1());
//		labTxtContact.setText(customerInfo.getLxr());
		labTxtPhone.setText(customerInfo.getLxdh());
		labTxtSendGoodsAddress.setText(customerInfo.getStreet());
		labTxtOrderGoodsDate.setText(customerInfo.getOrderReportInfoBean().getXqriqi()
				.substring(0, 10));
		labTxtFillOrderDate.setText(customerInfo.getOrderReportInfoBean().getCreatetime()
				.substring(0, 10));
		labTxtHandOrderId.setText(customerInfo.getOrderReportInfoBean().getOrderno());
		
		
		if(null!=customerInfo.getIsCar()&&"Y".equals(customerInfo.getIsCar())){
			chkIsCar.setChecked(true);
		}else{
			chkIsCar.setChecked(false);
			
		}
		
		
		
		labTxtOrderId.setText(customerInfo.getOrderReportInfoBean().getOrderid());
		labTxtOrderTotalPrice.setText(customerInfo.getOrderReportInfoBean().getTotalmoney());
		labTxtOrderTotalPriceNoRate.setText(customerInfo.getOrderReportInfoBean()
				.getTotalmoneynorate());
		labTxtOrderSendTotalPrice
				.setText(customerInfo.getOrderReportInfoBean().getTotalsendmoney());
		labTxtOrderSendTotalPriceNoRate.setText(customerInfo.getOrderReportInfoBean()
				.getTotalsendmoneynorate());

		LinearLayout.LayoutParams layoutParamsLab = new LinearLayout.LayoutParams(0,
				LayoutParams.WRAP_CONTENT);
		layoutParamsLab.setMargins(0, 0, 3, 0);
		layoutParamsLab.weight = 7;
		TextView tvLab = new TextView(OrderInfoView.this);
		tvLab.setGravity(Gravity.RIGHT);
		tvLab.setTextSize(15);

		LinearLayout.LayoutParams layoutParamsTxt = new LinearLayout.LayoutParams(0,
				LayoutParams.WRAP_CONTENT);
		layoutParamsTxt.setMargins(0, 0, 10, 0);
		layoutParamsTxt.weight = 25;
		TextView tvTxt = new TextView(OrderInfoView.this);
		tvTxt.setGravity(Gravity.LEFT);
		tvTxt.setTextSize(15);

		if (null != customerInfo.getJxsid() && null != customerInfo.getJxsname()) {
			tvLab.setText("经销商:");
			tvTxt.setText(customerInfo.getJxsid() + " " + customerInfo.getJxsname());
		} else if (null != customerInfo.getFxsid() && null != customerInfo.getFxsname()) {
			tvLab.setText("分销商:");
			tvTxt.setText(customerInfo.getFxsid() + " " + customerInfo.getFxsname());
		}
		layChangeBusiness.addView(tvLab, layoutParamsLab);
		layChangeBusiness.addView(tvTxt, layoutParamsTxt);

		if (null != customerInfo.getTosend() && "Y".equals(customerInfo.getTosend())) {
			labTxtFaxstate.setText(customerInfo.getFaxstate());
			labTxtFax.setText(customerInfo.getFax());
		}

		orderInfoAdapter = new OrderInfoAdapter(this, listOrderDetail);
		listView.setAdapter(orderInfoAdapter);
	}

	private void addListener() {
		btnFax.setOnClickListener(btnListner);
	}

	private class OrderInfoAdapter extends BaseAdapter {

		private Context context = null;
		private List<OrderDetailBean> list = null;

		public OrderInfoAdapter(Context context, List<OrderDetailBean> list) {
			this.context = context;
			this.list = list;
		}

		public int getCount() {
			return list.size();
		}

		public OrderDetailBean getItem(int position) {
			return list.get(position);
		}

		@SuppressWarnings("unused")
		public List<OrderDetailBean> getItemAll() {
			return list;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			String inflater = Context.LAYOUT_INFLATER_SERVICE;
			LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(inflater);
			LinearLayout linearLayout = (LinearLayout) layoutInflater.inflate(
					R.layout.order_info_view_lay, null);
			;
			if (list.get(position) instanceof OrderDetailBean) {
				OrderDetailBean bean = list.get(position);
				TextView textView = ((TextView) linearLayout.findViewById(R.id.labTxtOrderInfoSKU));
				textView.setText(bean.getBismt());
				textView = ((TextView) linearLayout.findViewById(R.id.labTxtTotalprice));
				textView.setText(bean.getTotalprice() + "元");
				textView = ((TextView) linearLayout.findViewById(R.id.labTxtOrderInfoNum));
				textView.setText(bean.getNum() + list.get(position).getUnitInfo().getUnitname());
				textView = ((TextView) linearLayout.findViewById(R.id.labTxtOrderPrice));
				textView.setText(bean.getOrderprice() + "元");
				textView = ((TextView) linearLayout.findViewById(R.id.labTxtOrderInfoSendNum));
//				textView.setText(bean.getSendnum() + "箱");
				if(bean.getSendnum()!=null&&"0".equals(bean.getSendnum())){
					textView.setText(bean.getSendnum());
				}else if(null!=bean.getSendunitname()&&!"".equals(bean.getSendunitname())){
					textView.setText(bean.getSendnum() + bean.getSendunitname());
				}else{
					textView.setText(bean.getSendnum());
				}
				
				textView = ((TextView) linearLayout.findViewById(R.id.labTxtOrderSendPrice));
				textView.setText(bean.getSendprice() + "元");
				textView = ((TextView) linearLayout.findViewById(R.id.labTxtOrderSendTotalPrice));
				textView.setText(bean.getTotalsendprice() + "元");
				textView = ((TextView) linearLayout.findViewById(R.id.labTxtOrderComments));
				textView.setText(bean.getRemark());
			}
			return linearLayout;
		}
	}

	private View.OnClickListener btnListner = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
				case R.id.btnFax :
					bundle = new Bundle();
					bundle.putSerializable("customerInfo", customerInfo);
					intent = new Intent(OrderInfoView.this, OrderFaxSend.class);
					intent.putExtras(bundle);
					OrderInfoView.this.startActivityForResult(intent,
							CommonStatic.INTENT_REQUEST_CODE);
					break;
			}
		}
	};

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
			case CommonStatic.INTENT_REQUEST_CODE :
				if (resultCode == RESULT_OK) {
					bundle = data.getExtras();
					String result = bundle.getString("result");
					if (null != result && "OK".equals(result)) {
						String faxstate = bundle.getString("faxstate");
						String fax = bundle.getString("fax");
						labTxtFaxstate.setText(faxstate);
						labTxtFax.setText(fax);
					}
				}
				break;
			default :
				break;
		}
	}
}
